package com.hiking.cmdchat.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hiking.cmdchat.R;
import com.hiking.cmdchat.bean.PromptBean;

import java.util.List;

public class PromptAdapter extends RecyclerView.Adapter<PromptAdapter.ViewHolder> {
    private List<PromptBean> datas;

    public PromptAdapter(List<PromptBean> datas) {
        this.datas = datas;
    }

    @Override
    public PromptAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_prompt_view, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        PromptBean bean = datas.get(position);
        holder.setData(bean);
        if (mListener != null) {
            //绑定点击事件
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //把条目的位置回调回去
                    mListener.onItemClick(position);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView contentView;

        public ViewHolder(View itemView) {
            super(itemView);

            contentView = (TextView) itemView.findViewById(R.id.tv_content);
        }

        public void setData(PromptBean bean) {
            if (null == contentView) return;
            contentView.setText(bean.getContent());
        }

    }

    /**
     * 定义一个接口
     */
    public interface ItemClickListener {
        void onItemClick(int position);
    }

    public ItemClickListener mListener;

    public void setOnItemClickListener(ItemClickListener listener) {
        this.mListener = listener;
    }


}
